package com.link2room.rodimus.controller.manager;

import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.dto.threadlocal.BaseThreadLocal;
import com.link2room.rodimus.dto.param.manager.*;
import com.link2room.rodimus.facade.IMallManagerFacadeService;
import com.link2room.sideswipe.permissionResouces.annotation.L2RPermissionResources;
import com.link2room.rodimus.dto.result.base.MallGoodBaseApiDto;
import com.link2room.rodimus.dto.result.base.MallOrderBaseDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.link2room.rodimus.constant.PermissionConstant.*;

/**
 * Created by VegetaKo on 2017/1/20.
 */
@Api(tags = "商城相关")
@RestController
@RequestMapping("/manager/Mall")
public class MallManagerController {

    @Resource
    private IMallManagerFacadeService mallManagerFacadeService;

    @ApiOperation(value = "商城订单列表", response = MallOrderBaseDto.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "状态", required = false, allowableValues = "NEEDPAY=待支付\nINIT=成功\nCANCEL=取消")
    })
    @RequestMapping(value = "/Orders", method = RequestMethod.GET)
    @L2RPermissionResources(value = mallOrders, name = "商品订单列表")
    public BaseResultDto lisetOrders(
            @RequestParam(value = "stas", required = false) String stas
    ) throws Exception {
        MallOrderManagerParamDto mallOrderManagerParamDto = new MallOrderManagerParamDto();
        mallOrderManagerParamDto.setStas(stas);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setQueryResult(mallManagerFacadeService.getOrders(mallOrderManagerParamDto));
        resDto.setResultCode(0);
        return resDto;
    }

    @ApiOperation(value = "商城订单详情修改")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "id", required = false, paramType = "path")
    })
    @RequestMapping(value = "/Orders/{id}", method = RequestMethod.PUT)
    @L2RPermissionResources(value = modifyMallOrder, name = "修改商城订单")
    public BaseResultDto modifyOrder(
            @RequestBody MallOrderManagerParamDto mallOrderManagerParamDto,
            @PathVariable("id") Long id) throws Exception {
        //MallOrderApiParamDto mallOrderApiParamDto = new MallOrderApiParamDto();
        if (mallOrderManagerParamDto.getId() == null) {
            mallOrderManagerParamDto.setId(id);
        }
        BaseResultDto resDto = new BaseResultDto();
        mallManagerFacadeService.modifyOrder(mallOrderManagerParamDto);
        //result.setQueryResult( mallApiFacadeService.getOrders(mallOrderApiParamDto ));
        resDto.setResultCode(0);
        return resDto;
    }

    @ApiOperation(value = "商城订单详情", response = MallOrderBaseDto.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "id", required = false, paramType = "path")
    })
    @RequestMapping(value = "/Orders/{id}", method = RequestMethod.GET)
    @L2RPermissionResources(value = mallOrder, name = "订单详情")
    public BaseResultDto getOrder(
            @PathVariable Long id) throws Exception {
        MallOrderManagerParamDto mallOrderManagerParamDto = new MallOrderManagerParamDto();
        mallOrderManagerParamDto.setId(id);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultInfo(mallManagerFacadeService.getOrder(mallOrderManagerParamDto));
        resDto.setResultCode(0);
        return resDto;
    }

    @ApiOperation(value = "商城商品列表", response = MallGoodBaseApiDto.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "goodCategory", value = "商品类目", required = false, paramType = "query"),
            @ApiImplicitParam(name = "isHalt", value = "是否禁用", required = false, paramType = "query"),
    })
    @RequestMapping(value = "/Goods", method = RequestMethod.GET)
    @L2RPermissionResources(value = mallGoods, name = "商城物品列表")
    public BaseResultDto listGoods(
            @RequestParam(value = "goodCategory", required = false) String goodCategory,
            @RequestParam(value = "isHalt", required = false) String isHalt) throws Exception {

        MallGoodsManagerParamDto mallGoodsManagerParamDto = new MallGoodsManagerParamDto();
        mallGoodsManagerParamDto.setGoodCategory(goodCategory);
        mallGoodsManagerParamDto.setIsHalt(isHalt);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setQueryResult(mallManagerFacadeService.getGoods(mallGoodsManagerParamDto));
        resDto.setResultCode(0);
        return resDto;
    }

    @ApiOperation(value = "商城商品详情", response = MallGoodBaseApiDto.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "goodCode", value = "商品代码", required = false, paramType = "path")
    })
    @RequestMapping(value = "/Goods/{goodCode}", method = RequestMethod.GET)
    public BaseResultDto getGood(
            @PathVariable("goodCode") String goodCode) throws Exception {
        MallGoodsFindParamDto paramDto = new MallGoodsFindParamDto();
        paramDto.setGoodCode(goodCode);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultInfo(mallManagerFacadeService.getGoodByCode(paramDto));
        resDto.setOptId(BaseThreadLocal.getOptId());
        resDto.setResultCode(0);
        return resDto;
    }


    //    @RequestMapping(value = "/Goods/{id}", method = RequestMethod.PUT)
//    @L2RPermissionResources(value = modifyMallGood, name = "修改商城物品")
//    public BaseResultDto modifyGood(
//            @RequestBody MallGoodsManagerParamDto mallGoodsManagerParamDto, @PathVariable("id") String id) throws Exception {
//        mallGoodsManagerParamDto.setId(Long.getLong(id));
//        BaseResultDto resDto = new BaseResultDto();
//        mallManagerFacadeService.modifyGood(mallGoodsManagerParamDto);
//        resDto.setResultCode(0);
//        return resDto;
//    }
    @RequestMapping(value = "/Goods/{goodCode}", method = RequestMethod.PUT)
    @ApiOperation(value = "商品更新")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "goodCode", value = "商品代码", required = false, paramType = "path")
    })
    public BaseResultDto updateMallGood(
            @RequestBody MallGoodsUpdateParamDto paramDto, @PathVariable("goodCode") String goodCode) throws Exception {
        paramDto.getMallGoodBaseDto().setGoodCode(goodCode);
        BaseResultDto resDto = new BaseResultDto();
        mallManagerFacadeService.updateMallGood(paramDto);
        resDto.setResultCode(0);
        return resDto;
    }

    @RequestMapping(value = "/Good", method = RequestMethod.POST)
    @ApiOperation(value = "商品创建")
    public BaseResultDto createMallGood(
            @RequestBody MallGoodCreateParamDto paramDto
    ) throws Exception {
        BaseResultDto resDto = new BaseResultDto();
        mallManagerFacadeService.createMallGood(paramDto);
        resDto.setResultCode(0);
        return resDto;
    }

    @RequestMapping(value = "/GoodTypes", method = RequestMethod.GET)
    @ApiOperation(value = "商品类型列表")
    public BaseResultDto getGoodTypes(

    ) throws Exception {

        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setQueryResult(mallManagerFacadeService.getMallGoodType(new BaseL2rParamDto()));
        return resDto;
    }


    @RequestMapping(value = "/GoodPrices", method = RequestMethod.GET)
    @ApiOperation(value = "商品价格")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "goodCode", value = "商品代码", required = false, paramType = "query"),
            @ApiImplicitParam(name = "tag", value = "标签", required = false, paramType = "query"),
            @ApiImplicitParam(name = "priceCode", value = "价格代码", required = false, paramType = "query"),
            @ApiImplicitParam(name = "priceType", value = "类型", required = false, paramType = "query"),
    })
    public BaseResultDto getGoodPrices(
            @RequestParam(name = "goodCode",required = false)String goodCode,
            @RequestParam(name = "tag",required = false)String tag,
            @RequestParam(name = "priceCode",required = false)String priceCode,
            @RequestParam(name = "priceType",required = false)String priceType
            ) throws Exception {
        MallGoodPriceFindParamDto priceFindParamDto = new MallGoodPriceFindParamDto();
        priceFindParamDto.setGoodCode(goodCode);
        priceFindParamDto.setPriceCode(priceCode);
        priceFindParamDto.setPriceType(priceType);
        priceFindParamDto.setTag(tag);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setQueryResult(mallManagerFacadeService.mallGoodPrices(priceFindParamDto));
        return resDto;
    }
    @RequestMapping(value = "/GoodPrices", method = RequestMethod.POST)
    @ApiOperation(value = "商品价格创建")

    public BaseResultDto createGoodPrices(
            @RequestBody MallGoodPriceCreateParamDto paramDto
    ) throws Exception {
        mallManagerFacadeService.createMallGoodPrices(paramDto);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        return resDto;
    }

    @RequestMapping(value = "/GoodPrices/{priceCode}", method = RequestMethod.PUT)
    @ApiOperation(value = "商品价格更新")
    public BaseResultDto updateGoodPrice(
            @RequestBody MallGoodPriceUpdateParamDto paramDto,
            @PathVariable("priceCode") String priceCode) throws Exception {
        paramDto.getMallGoodPrice().setPriceCode(priceCode);
        mallManagerFacadeService.updateMallGoodPrices(paramDto);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        return resDto;
    }


    @RequestMapping(value = "/GoodPriceTypes", method = RequestMethod.GET)
    @ApiOperation(value = "商品价格类型")
    public BaseResultDto getGoodPriceType(
    ) throws Exception {
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setQueryResult(mallManagerFacadeService.mallGoodPriceTpyes(new BaseL2rParamDto()));
        return resDto;
    }
    @RequestMapping(value = "/GoodPrices/{priceCode}", method = RequestMethod.DELETE)
    @ApiOperation(value = "商品价格删除")
    public BaseResultDto deletePrice(
            @PathVariable("priceCode") String priceCode) throws Exception {
        MallGoodPriceDeleteParamDto paramDto = new MallGoodPriceDeleteParamDto();
        paramDto.setPriceCode(priceCode);
        mallManagerFacadeService.deleteMallGoodPrice(paramDto);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        return resDto;
    }



}
